<template>
  <div class="login-box">
    <el-card shadow="always" :body-style="{ padding: '0px' }">
      <el-image class="logo" :src="img('logo.png')" fit="cover" :lazy="true"></el-image>
      <el-row>
        <el-col :span="12">
          <el-image :src="img('mall-logo.png')" fit="cover" :lazy="true"></el-image>
        </el-col>
        <el-col :span="12">
          <el-form :model="loginUser" :rules="userRule" ref="loginForm">
            <h4 class="title">网博商城后台管理系统</h4>
            <el-form-item prop="username">
              <el-input
                v-model="loginUser.username"
                prefix-icon="el-icon-user"
                size="large"
                placeholder="请输入用户名"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginUser.password"
                show-password
                prefix-icon="el-icon-lock"
                placeholder="请输入密码"
                type="password"
                size="large"
              />
            </el-form-item>
            <el-form-item class="button">
              <el-button type="primary" size="large" @click="checkUserLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { loginRquest } from "@/api/users.js";
import { userRule } from "@/rule/userRule.js";
import { Message } from "element-ui";

export default {
  name: "LoginView",
  data() {
    return {
      loginUser: {
        username: "",
        password: ""
      },
      userRule,
    };
  },
  methods: {
    ...mapActions(["setToken", "setUser"]), // 假设你的 vuex 有这两个 action
    img(name) {
      // 静态资源图片加载
      return require(`@/assets/${name}`);
    },
    checkUserLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return;
        const result = await loginRquest(this.loginUser);
        if (!result) return;
        this.setToken(result.token);
        this.setUser(result.user);
        this.$router.push({ name: "home" });
        Message.success("欢迎回来");
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-box {
  height: 100%;
  background-color: var(--vt-c-indigo);
}

.el-card {
  font-size: 0px;
  width: 800px;
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translate(-50%, 0%);
  overflow: unset;
}

.el-image {
  height: 400px;
  border-radius: 5px 0px 0px 5px;
}

.logo {
  width: 180px;
  height: 180px;
  position: absolute;
  top: -100px;
  left: 50%;
  z-index: 2;
  background-color: var(--vt-c-white-soft);
  border: 8px solid var(--vt-c-indigo);
  border-radius: 50%;
  padding: 20px;
  box-sizing: border-box;
  transform: translate(-50%, 0px);
}

.el-form {
  padding: 30px;
  padding-top: 40px;
  height: 100%;
  width: 100%;
  box-sizing: border-box;

  .el-form-item {
    margin-bottom: 30px;
  }

  .button {
    margin-bottom: 0px;
  }

  .el-button {
    width: 100%;
  }
}

.title {
  font-size: 20px;
  margin: 24px;
  text-align: center;
}
</style>
